package com.wxy.demo.controller;

import com.wxy.demo.core.AssertUtils;
import com.wxy.demo.core.IdDto;
import com.wxy.demo.core.PageResult;
import com.wxy.demo.core.QueryPage;
import com.wxy.demo.dao.DataSourceInfoDao;
import com.wxy.demo.dao.InterfaceInfoDao;
import com.wxy.demo.entity.DataSourceInfo;
import com.wxy.demo.util.DbUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 *
 * </p>
 *
 * @author e-Xiaoyuan.Wang
 * @since 2022/1/14 10:14
 */
@Transactional
@RestController
@RequestMapping("data-source-info")
public class DataSourceInfoController {

    @Autowired
    private DataSourceInfoDao dataSourceInfoDao;

    @Autowired
    private InterfaceInfoDao interfaceInfoDao;

    @PostMapping("/save")
    private Object save(@RequestBody DataSourceInfo dataSourceInfo) {
        AssertUtils.notBlank(dataSourceInfo.getName(), "数据源名称不能为空");
        DataSourceInfo dataSource = dataSourceInfoDao.findByName(dataSourceInfo.getName());
        AssertUtils.isTrue((dataSourceInfo.getId() == null && dataSource == null) ||
                        (dataSourceInfo.getId() != null && Objects.equals(dataSourceInfo.getId(), dataSource.getId())),
                "数据源名称不能重复");
        return dataSourceInfoDao.save(dataSourceInfo);
    }

    @PostMapping("/connect")
    private Object connect(@RequestBody DataSourceInfo dataSourceInfo) {
        AssertUtils.notBlank(dataSourceInfo.getType(), "数据库类型不能为空");
        Connection conn = null;
        try {
            conn = DbUtils.getConn(dataSourceInfo);
        } catch (ClassNotFoundException | SQLException e) {

        }
        boolean isConnect = conn != null;
        DbUtils.close(conn);
        return isConnect;
    }

    @PostMapping("/deleteById")
    private Object deleteById(@RequestBody IdDto dto) {
        dataSourceInfoDao.deleteById(dto.getId());
        interfaceInfoDao.deleteAllByDataSourceId(dto.getId());
        return true;
    }

    @PostMapping("/findById")
    private Object findById(@RequestBody IdDto dto) {
        return dataSourceInfoDao.findById(dto.getId());
    }

    @PostMapping("/findPage")
    private Object findPage(@RequestBody QueryPage<DataSourceInfo> queryPage) {
        Pageable pageable = PageRequest.of(queryPage.getPageNum() - 1, queryPage.getPageSize(),
                Sort.by(Sort.Direction.DESC, "id"));
        DataSourceInfo query = Optional.ofNullable(queryPage.getQueryModel()).orElse(new DataSourceInfo());
        ExampleMatcher matching = ExampleMatcher.matching().withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());
        return new PageResult<>(dataSourceInfoDao.findAll(Example.of(query, matching), pageable));
    }
}
